package com.example.shop.commodity.controller;

import com.example.shop.base.BaseController;
import com.example.shop.base.Result;
import com.example.shop.commodity.entity.ShopCommodity;
import com.example.shop.commodity.service.ShopCommodityService;
import com.example.shop.domain.wx.WxUser;
import com.example.shop.wx.service.WxUserService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.example.shop.commodity.entity.ShopOrder;
import com.example.shop.commodity.service.ShopOrderService;


/**
 * @author hcj
 * @date 2021-09-21 19:06:20
 */
@Controller
@RequestMapping(value = "/shop/order")
public class ShopOrderController extends BaseController {

    @Autowired
    private ShopOrderService shopOrderService;
    @Autowired
    private ShopCommodityService commodityService;
    @Autowired
    WxUserService userService;
    @Value("${system.profile.http}")
    protected String picture_url;


    @ModelAttribute
    public ShopOrder get(@RequestParam(required = false) String id) {
        if (StringUtils.isNotBlank(id)) {
            ShopOrder shopOrder = new ShopOrder();
            shopOrder.setId(id);
            return shopOrderService.get(shopOrder);
        } else {
            return new ShopOrder();
        }
    }

    @RequestMapping(value = "/get")
    @ResponseBody
    public ShopOrder getById(ShopOrder shopOrder) {
        return shopOrder;
    }

    @RequestMapping(value = "/list")
    public String list(ShopOrder shopOrder) {
        return "shop/order/order_list";
    }

    @RequestMapping(value = "/info")
    @ResponseBody
    public List<ShopOrder> info(ShopOrder shopOrder) {
        return shopOrderService.findList(shopOrder);
    }


    @RequestMapping(value = "/findPage")
    @ResponseBody
    public PageInfo<ShopOrder> findPage(ShopOrder shopOrder) {
        ObjectMapper mapper = new ObjectMapper();
        PageInfo<ShopOrder> pageInfo = shopOrderService.findPage(shopOrder);
        pageInfo.getList().forEach(i -> {
            WxUser user = userService.get(i.getUid());
            if (user != null) {
                i.setUid(user.getLoginName());
            }
            if (StringUtils.isNoneBlank(i.getAid())) {
                try {
                    Map<String, Object> map = mapper.readValue(i.getAid(), Map.class);
                    String address = map.get("provinceName").toString() + map.get("cityName") + map.get("countyName") + map.get("detailInfo");
                    i.setAid(address);
                    i.setUserName(map.get("userName").toString());
                    i.setTelNumber(map.get("telNumber").toString());
                } catch (IOException exception) {
                    exception.printStackTrace();
                }
            }
            try {
                List<Map<String, Object>> list = mapper.readValue(i.getCommodity(), List.class);
                i.setInfo(list);
            } catch (IOException exception) {
                exception.printStackTrace();
            }
            String[] strings = i.getCid().split(",");
            for (String string : strings) {
                String[] s = string.split("-");
                for (Map<String, Object> map : i.getInfo()) {
                    if (s[0].equals(map.get("id"))) {
                        map.put("number", s[1]);
                        continue;
                    }
                }
            }
        });
        return pageInfo;
    }

    @RequestMapping(value = "/save")
    @ResponseBody
    public Result save(ShopOrder shopOrder) {
        shopOrderService.save(shopOrder);
        return Result.success("保存成功");
    }

    @RequestMapping(value = "/logicalDeletion", method = RequestMethod.POST)
    @ResponseBody
    public Result logicalDeletion(@RequestParam("id") String id) {
        shopOrderService.logicalDeletion(id);
        return Result.success();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@RequestParam("ids") String[] ids) {
        shopOrderService.delete(ids);
        return Result.success();
    }

}